<?php
namespace Yzadmin\Controller;
use Think\Controller;
class TixianController extends CkController
{
   //提现列表
   public function index()
   { 
     $status = (int) I('get.status');
     if($status){ $where['status']=$status; }
     if($status!==1) { $order="id desc"; }else{ $order = "id asc"; }

     $count = M("tixian")->where($where)->count(1);// 查询满足要求的总记录数
     $Page  = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
     $Page->setConfig('prev', "上一页");//上一页
     $Page->setConfig('next', '下一页');//下一页
     $Page->setConfig('first', '首页');//第一页
     $Page->setConfig('last', "末页");//最后一页
     $Page ->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
 
     $data['tixian']=D("Tixian")->where($where)->limit($Page->firstRow.','.$Page->listRows)->order($order)->select();
     $zonger= M("tixian")->field("id,SUM(price) as zonger")->where($where)->select();
     $data['zonger']= $zonger[0]['zonger'];
     $data['page'] = $Page->show();// 分页显示输出
     $data['title']="提现列表";
     $data['status']= $status;
     $this->assign($data);
     $this->display();
   }    
   
   
   //提现处理
   public function tixian_do(){
     $data=I('get.');
     $status=(int) $data['status'];
     //判断处理 1 为未处理 2: 处理失败 3 拒绝提现 4:处理成功
     //处理失败  余额回滚
     //拒绝处理  进入冻结资金
     //处理成功  已支付金额+
     if($status<=1 || $status>=5){
        $this->error("状态错误");
     }
     //判断一下这个提现订单是不是已经处理过了 如果处理过了 拒绝在处理
     $tixian=M("tixian")->where(array("id"=>$data['id'],'status'=>1))->find();
     if(!$tixian){
        $this->error("已处理提现,不可以重复处理.");
     }
     //提现处理失败
     if($status==2){
        //开启事务
        $M=M("tixian");
        $M->startTrans();
        $tixian_update=$M->where("id=".$data['id'])->setField('status',2);
        $user_update= M("user")->where("id=".$data['uid'])->setField(array(
           'balance'=>array("exp","balance+".$tixian['price'])
           ));
        if($tixian_update && $user_update){
            $M->commit();
            $this->success("处理成功");
        }else{
            $M->rollback();
            YZ_LOG($M->error());
            $this->error("处理失败");
        }
     }

     //拒绝提现
     if($status==3){
        $M=M("tixian");
        $M->startTrans();
        $tixian_update=$M->where("id=".$data['id'])->setField('status',3);
        $user_update= M("user")->where("id=".$data['uid'])->setField(array(
           'dongjie'=>array("exp","dongjie+".$tixian['price'])
           ));
        if($tixian_update && $user_update){
            $M->commit();
            $this->success("处理成功");
        }else{
            $M->rollback();
            YZ_LOG($M->error());
            $this->error("处理失败");
        }        
     }
     
     //提现成功
     if($status==4){
        $M=M("tixian");
        $M->startTrans();
        $tixian_update=$M->where("id=".$data['id'])->setField('status',4);
        $user_update= M("user")->where("id=".$data['uid'])->setField(array(
           'paid'=>array("exp","paid+".$tixian['price'])
           ));
        if($tixian_update && $user_update){
            $M->commit();
            $this->success("处理成功");
        }else{
            $M->rollback();
            YZ_LOG($M->error());
            $this->error("处理失败");
        }        
     }     
     

   }






}